package com.yh.bill.controller;

import com.github.pagehelper.PageInfo;
import com.yh.bill.pojo.Bill;
import com.yh.bill.pojo.ResultData;
import com.yh.bill.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName BillController
 * @Description 控制器
 * @Author 0715-YuHao
 * @Date 2020/10/17 12:37
 */
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService service;

    @GetMapping("/listPage")
    public ResultData listPage(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int limit,
                               Bill bill) {
        PageInfo<Bill> info = service.listPage(bill, page, limit);
        return new ResultData(0, "查询成功！", info.getTotal(), info.getList());
    }

    @GetMapping("/list")
    public List<Bill> list(Bill bill) {
        return service.list(bill);
    }

    @GetMapping("/{id}")
    public Bill queryById(@PathVariable Long id) {
        return service.get(id);
    }

    @PostMapping("/add")
    public ResultData add(Bill bill) {
        if (service.add(bill) > 0) {
            return new ResultData(0, "添加成功！", null, null);
        }else {
            return new ResultData(-1, "添加失败！", null, null);
        }
    }

    @PutMapping("/{id}")
    public ResultData update(@PathVariable Long id, Bill bill) {
        bill.setId(id);
        if (service.update(bill)>0) {
            return new ResultData(0, "修改成功！", null, null);
        }else {
            return new ResultData(-1, "修改失败！", null, null);
        }
    }

    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Long id) {
        if (service.delete(id) > 0) {
            return new ResultData(0, "删除成功！", null, null);
        }else {
            return new ResultData(-1, "删除失败！", null, null);
        }
    }
}
